<template>
  <div>
    <h2>数据概览</h2>
    <el-row :gutter="20" class="mb-4">
      <el-col :span="6">
        <el-card>
          <template #header>
            <span>传感器在线数</span>
          </template>
          <div class="card-content">
            <span class="number">{{ stats.onlineSensors }}</span>
            <span class="unit">个</span>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card>
          <template #header>
            <span>今日采集数据</span>
          </template>
          <div class="card-content">
            <span class="number">{{ stats.todayData }}</span>
            <span class="unit">条</span>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card>
          <template #header>
            <span>异常告警</span>
          </template>
          <div class="card-content">
            <span class="number error">{{ stats.alerts }}</span>
            <span class="unit">条</span>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card>
          <template #header>
            <span>平均pH值</span>
          </template>
          <div class="card-content">
            <span class="number">{{ stats.avgPH }}</span>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <el-row :gutter="20">
      <el-col :span="12">
        <el-card>
          <template #header>
            <span>24小时数据趋势</span>
          </template>
          <v-chart :option="trendOption" style="height: 300px" />
        </el-card>
      </el-col>
      <el-col :span="12">
        <el-card>
          <template #header>
            <span>土壤养分分布</span>
          </template>
          <v-chart :option="nutrientOption" style="height: 300px" />
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { getDashboardStats } from '../api/dashboard'

const stats = ref({
  onlineSensors: 156,
  todayData: 12480,
  alerts: 3,
  avgPH: 6.8
})

const trendOption = ref({
  xAxis: {
    type: 'category',
    data: ['00:00', '04:00', '08:00', '12:00', '16:00', '20:00', '24:00']
  },
  yAxis: {
    type: 'value',
    name: 'pH值'
  },
  series: [{
    data: [6.5, 6.7, 6.9, 7.1, 6.8, 6.6, 6.5],
    type: 'line',
    smooth: true
  }]
})

const nutrientOption = ref({
  tooltip: {
    trigger: 'item'
  },
  series: [{
    type: 'pie',
    radius: '50%',
    data: [
      { value: 35, name: '氮(N)' },
      { value: 30, name: '磷(P)' },
      { value: 20, name: '钾(K)' },
      { value: 15, name: '其他' }
    ]
  }]
})

onMounted(async () => {
  try {
    const data = await getDashboardStats()
    stats.value = { ...stats.value, ...data }
  } catch (error) {
    console.error('获取统计数据失败:', error)
  }
})
</script>

<style scoped>
.number {
  font-size: 32px;
  font-weight: bold;
  color: #409EFF;
}

.number.error {
  color: #F56C6C;
}

.unit {
  font-size: 14px;
  color: #909399;
  margin-left: 5px;
}

.card-content {
  text-align: center;
  padding: 10px 0;
}
</style>
